//
//  CatalogController.m
//  悦读
//
//  Created by dllo on 15/5/27.
//  Copyright (c) 2015年 贾建兴. All rights reserved.
//

#import "CatalogController.h"
#import "BookReadController.h"
@interface CatalogController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *catalog;
@property (nonatomic, retain) UISegmentedControl *seg;
@end

@implementation CatalogController

- (void)dealloc
{
    [_array release];
    [_opposedCatalog release];
    [_catalog release];
    [_catalogArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.catalogArray = [NSMutableArray array];
        self.opposedCatalog = [NSMutableArray array];
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBarHidden = NO;
    for (int i = 0; i < self.catalogArray.count; i++) {
        [self.opposedCatalog addObject:[self.catalogArray objectAtIndex:self.catalogArray.count - i - 1]];
    }
    [self.array addObjectsFromArray:self.catalogArray];
    self.navigationController.navigationBar.translucent = NO;
    [self createView];
    
}


- (void)createView
{
    NSArray *array = @[@"正序", @"倒序"];
    self.seg = [[UISegmentedControl alloc] initWithItems:array];
    self.seg.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
    self.seg.tintColor = [UIColor lightGrayColor];
    
    self.seg.backgroundColor = [UIColor whiteColor];
    self.seg.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式
    // 点击后是否恢复原样
    self.seg.momentary = YES;
    _seg.layer.borderColor = [[UIColor cyanColor] CGColor];
    [self.view addSubview:self.seg];
    self.seg.selectedSegmentIndex = 0;
    [self.seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [self.seg release];
    
    
    self.catalog = [[UITableView alloc] init];
    UIDevice *device = [UIDevice currentDevice];
    if ([device.systemVersion compare:@"5.0"] > 0) {
        self.catalog.frame = CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 64 - 60);
    } else {
        self.catalog.frame = CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 44 - 60);
    }
    self.catalog.rowHeight = 50;
    
    self.catalog.dataSource = self;
    self.catalog.delegate = self;
    [self.view addSubview:self.catalog];
    [self.catalog release];
    
}


- (void)segAction:(UISegmentedControl *)seg
{
    
    switch (seg.selectedSegmentIndex) {
        case 0:
        {
            
            self.array = self.catalogArray;
            [self.catalog reloadData];
        }
            
            break;
        case 1:
        {
            
            self.array = self.opposedCatalog;
            [self.catalog reloadData];
        }
            break;
        default:
            break;
    }
}




#pragma mark -
#pragma mark tableView的协议

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentify];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    //取出章节名
     cell.textLabel.text = [[self.array objectAtIndex:indexPath.row] objectForKey:@"chapter_name"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookReadController *bookRead = [[BookReadController alloc] init];
    
    [self.navigationController popViewControllerAnimated:YES];
    [self.delegate readChapter:indexPath.row];
    [bookRead release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
